﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SSOCommon;
using SSOServer.Config;

namespace SSOServer
{
    public partial class Login : System.Web.UI.Page
    {
        protected string ReturnUrl
        {
            get
            {
                if (Request.QueryString[ArgumentConstant.REDIRECTURL] == null)
                {

                }
                return Request.QueryString[ArgumentConstant.REDIRECTURL];
            }
        }

        private bool LoginOn(string userName, string password)
        {
            return true;
        }

        private void Authentication()
        {
            string token = String.Empty;
            if (LoginOn(this.tbUserName.Text, this.tbPassword.Text))
            {
                token = CoreServices.GenerateToken();
                StateManager.Add(token, new UserInfo() { ID = token, Name = this.tbUserName.Text });
            }
            Dictionary<string, object> args = new Dictionary<string, object>();
            args.Add(ArgumentConstant.ACTION, (int)SSOAction.Login);
            args.Add(ArgumentConstant.SENDER, (int)SenderSource.Server);
            args.Add(ArgumentConstant.TOKEN, token);
            Response.Clear();
            //Response.Write(Tools.Redirect(ReturnUrl, args));
            Response.StatusCode = 302;
            Response.RedirectLocation = Tools.GenerateUrl(ReturnUrl, args);
            Response.End();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_OnClick(object sender, EventArgs e)
        {
            Authentication();
        }
    }
}